package com.java.yz.day09.ketang;

/*
    把数组中的数据按照指定个格式拼接成一个字符串
    举例：int[] arr = {1,2,3};	输出结果：[1, 2, 3]

    字符串反转
    举例：键盘录入”abc”		输出结果：”cba”

    统计大串中小串出现的次数（作业）
    举例：在字符串” woaijavawozhenaijavawozhendeaijava
    wozhendehenaijavaxinbuxinwoaijavagun”中java出现了5次
 */
import java.util.Scanner;
public class StringText1 {
    public static void main(String[] args) {
        int[] arr = {1,2,3};
        ArrayPrint(arr);

        //键盘录入”abc”		输出结果：”cba”
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入字符串：");
        String next = scanner.next();
        System.out.println("反转前："+next);

        //方法一  倒着遍历，拼接字符串
        String news="";
        for (int i = next.length()-1; i >=0 ; i--) {
            news+=next.charAt(i);
        }
        System.out.println("反转后：");
        System.out.println(news);

        //方法二 将字符串转成数组，逆序在转字符串
        char[] chars =next.toCharArray();
        for (int strat = 0,end= chars.length-1; strat < end; strat++,end--) {
            char temp =chars[strat];
            chars[strat]=chars[end];
            chars[end]=temp;
        }
        System.out.println("反转后：");
        System.out.println(chars);

        //每找到下一次就开始截取  使用indexOf来截取
        String Big="woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavagun";
        String Sml="java";
        //定义一个记录数
        int count=0;
        int num=Big.indexOf(Sml); //不存在就返回-1
        while (num!=-1){
            count++;
            //截取，从找到一次就开始往后截取
            Big=Big.substring(num+Sml.length());
            //重新找小串
            num=Big.indexOf(Sml);
        }
        System.out.println("java出现次数："+count);

    }
    public static void ArrayPrint(int[] array){
        for (int i = 0; i < array.length; i++) {
            if(i==0){
                System.out.print("["+array[0]+",");
            }
            else if (i==array.length-1){
                System.out.println(array[array.length-1]+"]");
            }
            else {
                System.out.print(array[i]+",");
            }
        }
    }
}
